using Senparc.Weixin.Entities;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.QY.AdvancedAPIs.Media;
using Senparc.Weixin.QY.CommonAPIs;
using Senparc.Weixin.QY.Entities;
using System;
using System.Collections.Generic;
using System.IO;
namespace Senparc.Weixin.QY.AdvancedAPIs
{
	public static class MediaApi
	{
		public static UploadTemporaryResultJson Upload(string accessToken, UploadMediaFileType type, string media, int timeOut = 10000)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", accessToken, type.ToString());
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary["media"] = media;
			return Post.PostFileGetJson<UploadTemporaryResultJson>(url, null, dictionary, null, null, timeOut);
		}
		public static void Get(string accessToken, string mediaId, Stream stream)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}", accessToken, mediaId);
			Senparc.Weixin.HttpUtility.Get.Download(url, stream);
		}
		public static UploadForeverResultJson AddMpNews(string accessToken, int agentId, int timeOut = 10000, params MpNewsArticle[] mpNewsArticles)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/material/add_mpnews?access_token={0}", accessToken);
			var data = new
			{
				agentid = agentId,
				mpnews = new
				{
					articles = mpNewsArticles
				}
			};
			return CommonJsonSend.Send<UploadForeverResultJson>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static UploadForeverResultJson AddMaterial(string accessToken, UploadMediaFileType type, int agentId, string media, int timeOut = 10000)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/material/add_material?agentid={1}&type={2}&access_token={0}", accessToken, agentId, type);
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary["media"] = media;
			return Post.PostFileGetJson<UploadForeverResultJson>(url, null, dictionary, null, null, timeOut);
		}
		public static GetForeverMpNewsResult GetForeverMpNews(string accessToken, int agentId, string mediaId)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/material/get?access_token={0}&media_id={1}&agentid={2}", accessToken, mediaId, agentId);
			return CommonJsonSend.Send<GetForeverMpNewsResult>(null, urlFormat, null, CommonJsonSendType.GET, 10000);
		}
		public static void GetForeverMaterial(string accessToken, int agentId, string mediaId, Stream stream)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/material/get?access_token={0}&media_id={1}&agentid={2}", accessToken, mediaId, agentId);
			Senparc.Weixin.HttpUtility.Get.Download(url, stream);
		}
		public static QyJsonResult DeleteForeverMaterial(string accessToken, int agentId, string mediaId)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/material/del?access_token={0}&agentid={1}&media_id={2}}", accessToken, agentId, mediaId);
			return CommonJsonSend.Send<QyJsonResult>(null, urlFormat, null, CommonJsonSendType.GET, 10000);
		}
		public static UploadForeverResultJson UpdateMpNews(string accessToken, string mediaId, int agentId, int timeOut = 10000, params MpNewsArticle[] mpNewsArticles)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/material/update_mpnews?access_token={0}", accessToken);
			var data = new
			{
				agentid = agentId,
				media_id = mediaId,
				mpnews = new
				{
					articles = mpNewsArticles
				}
			};
			return CommonJsonSend.Send<UploadForeverResultJson>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static GetCountResult GetCount(string accessToken, int agentId)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/material/count?access_token={0}&agentid={1}", accessToken, agentId);
			return CommonJsonSend.Send<GetCountResult>(null, urlFormat, null, CommonJsonSendType.GET, 10000);
		}
		public static BatchGetMaterialResult BatchGetMaterial(string accessToken, UploadMediaFileType type, int agentId, int offset, int count, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/material/batchget?access_token={0}", accessToken);
			var data = new
			{
				type = type.ToString(),
				agentid = agentId,
				offset = offset,
				count = count
			};
			return CommonJsonSend.Send<BatchGetMaterialResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
	}
}
